package com.yylending.las.splash;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.animation.AlphaAnimation;
import android.widget.Button;
import android.widget.ImageView;

import com.trello.rxlifecycle.components.support.RxAppCompatActivity;
import com.yyfax.common.indicator.RoundCornerIndicator;
import com.yylending.las.R;
import com.yylending.las.YJKD;
import com.yylending.las.main.MainActivity;
import com.yylending.las.common.utils.preferenceutils.PreferenceConstants;

/**
 * Created by Pengrf on 2016/11/18 14:25.
 *
 * @更新时间 2016/11/18
 */
public class GuideActivity extends RxAppCompatActivity {
	private final static String START_TYPE = "start_type";

	public final static int MODE_SPLASH = 0;
	public final static int MODE_VIEW = 1;

	private ViewPager mViewpager;

	private Button mButton;
	private RoundCornerIndicator mCircle;
	private RoundCornerIndicator mRectangle;

	private AlphaAnimation mAlphaAnimation;
	private int mMode = MODE_SPLASH;
	private int[] mImage = {R.drawable.bg_guide_1, R.drawable.bg_guide_2, R.drawable.bg_guide_3};

	public static void start(Activity aty, int mode) {
		Intent intent = new Intent(aty, GuideActivity.class);
		intent.putExtra(START_TYPE, mode);
		aty.startActivity(intent);
	}

	@Override
	protected void onCreate(@Nullable Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
		setContentView(R.layout.activity_guide);
		initView();
		initAnim();

		initData();
		Intent i = getIntent();
		if (i != null) {
			mMode = i.getIntExtra(START_TYPE, MODE_SPLASH);
		}
	}

	private void initView() {
		mViewpager = (ViewPager) findViewById(R.id.guide_viewpager);
		mButton = (Button) findViewById(R.id.guide_start_btn);
		mCircle = (RoundCornerIndicator) findViewById(R.id.guide_indicator);
		mRectangle = (RoundCornerIndicator) findViewById(R.id.guide_indicator_rectangle);
	}

	private void initAnim() {
		mAlphaAnimation = new AlphaAnimation(0.3f, 1);
		mAlphaAnimation.setDuration(400);
	}

	private void initData() {
		mViewpager.setAdapter(mPagerAdapter);
		mViewpager.setOnPageChangeListener(mOnPageChangeListener);
		mButton.setOnClickListener(mOnClickListener);
		mCircle.setViewPager(mViewpager);
		mRectangle.setViewPager(mViewpager);
	}

	private PagerAdapter mPagerAdapter = new PagerAdapter() {
		@Override
		public int getCount() {
			return mImage.length;
		}

		@Override
		public boolean isViewFromObject(View view, Object object) {
			return view == object;
		}

		@Override
		public Object instantiateItem(ViewGroup container, int position) {
			ImageView child = new ImageView(GuideActivity.this);
			child.setImageResource(mImage[position]);
			child.setScaleType(ImageView.ScaleType.CENTER_CROP);
			container.addView(child);
			return child;
		}

		@Override
		public void destroyItem(ViewGroup container, int position, Object object) {
			container.removeView((View) object);
		}
	};

	private ViewPager.OnPageChangeListener mOnPageChangeListener = new ViewPager.OnPageChangeListener() {
		@Override
		public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
		}

		@Override
		public void onPageSelected(int position) {
			if (position == mImage.length - 1) {
				mButton.setVisibility(View.VISIBLE);
				mButton.startAnimation(mAlphaAnimation);
			} else {
				mButton.setVisibility(View.GONE);
			}
		}

		@Override
		public void onPageScrollStateChanged(int state) {
		}
	};

	private View.OnClickListener mOnClickListener = new View.OnClickListener() {
		@Override
		public void onClick(View view) {
			if (mMode == MODE_SPLASH) {
				YJKD.getInstance().getPreferenceManager().savePreference(PreferenceConstants.KEY_FIRST_TIME_TO_LAUNCH, true);
				MainActivity.start(GuideActivity.this);
			}
			finish();
		}
	};
}
